//
//  HeartRateWeekMonthChartView.swift
//  DaRings
//
//  Created by 尹琼 on 2023/2/17.
//

import UIKit
import Charts
import MYModule

open class HeartRateWeekMonthChartView: View {
   
 
    var dateString: String = "--" {
    
        didSet {
            timeLabel.text = dateString
        }
        
    }
    
    private(set) var date: Date = Date()
    
    private(set) var type: HeartType?
    
    private(set) var max: Int = 100
    private(set) var min: Int = 0
    
   
    private(set) var avg: Int = 80 {
        
        didSet {
            
            dataLabel.text = avg.normalTitle
            
        }
    }
    
    
   private(set) var barDatas: [HeartBarData]? {
        
        didSet {
            
            guard let barDatas = barDatas else {
                return
            }
            
            let color = UIColor.HeartRate.theme
            let hightlightColor = UIColor.HeartRate.highlight
            var datas: [(low: Double, high: Double)] = []
            
            barDatas.forEach {

                let tuple = (Double($0.min), Double($0.max))
                datas.append(tuple)

            }
            
            barChartView.setDoubleLineData(datas, lowBarColor: .clear, highBarColor: color, lowBarHighLichtColor: .clear, highBarHighLichtColor: hightlightColor, highlightEnabled: true)
        }
    }
    
    public let barChartView = DFBarChartView()
    
    open override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = UIColor.background
        
        let barConfig = DFBarChartViewConfig(xAxisLabelFont: Fonts.systemFont(ofSize: 10), xAxisLabelTextColor: UIColor.assist_D0D0D0, xAxisLineColor: .clear)
    
        barChartView.setConfig(barConfig)

        barChartView.leftAxis.labelTextColor = UIColor.assist_D0D0D0
        barChartView.leftAxis.axisLineColor = .clear
        barChartView.leftAxis.enabled = true
        barChartView.leftAxis.drawGridLinesEnabled = true
        barChartView.leftAxis.gridLineDashLengths = [3.0, 3.0] //线段类型
        barChartView.leftAxis.gridColor = UIColor.assist_D0D0D0 //网格线颜色
        barChartView.leftAxis.gridAntialiasEnabled = true//开启抗锯齿
        barChartView.roundRenderer?.rectCorner = .eachEntryTopAndBottom
        
        barChartView.leftAxis.forceLabelsEnabled = true

        resetBarCustomAxisMax()
        
        barChartView.highlightFullBarEnabled = false
       
        addSubview(dataLabel)
        addSubview(timeLabel)
        addSubview(unitLabel)
        addSubview(barChartView)
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .lastBaseline
        dataStack.contentMode = .center
        dataStack.addArrangedSubview(dataLabel)
        dataStack.addArrangedSubview(unitLabel)
        let labelStackView = StackView()
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        self.addSubview(labelStackView)
        labelStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(timeLabel)

        barChartView.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(20)
            make.leading.equalToSuperview()
            make.trailing.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        
        bindViewModel()
        
    }
    
    func bindViewModel() {
        
        barChartView.selectIndex.subscribe(onNext: {[weak self] i in
            
            guard let barDatas = self?.barDatas else {
                self?.dataLabel.text = "--"
                return
            }
            
            self?.dataLabel.text = barDatas[i].avg.normalTitle
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    
    public func bind(type: HeartType, date: Date, barDatas: [HeartBarData]?, allAvg: Int, YAxisMaxValue: Int, YAxisminValue: Int, YAxisLineCount: Int) {
        
    
        self.avg = allAvg
        self.max = YAxisMaxValue
        self.min = YAxisminValue

        self.type = type
        self.date = date
        
        barChartView.setXAxis(valueFormatter: type.xValueFormatter(date: date))
        barChartView.setBarCount(type.barCount(date: date))
        
        barChartView.leftAxis.labelCount = 4
        barChartView.leftAxis.forceLabelsEnabled = true
        
        self.barDatas = barDatas
        
    }
    
    
    func resetBarCustomAxisMax() {
        
        barChartView.rightAxis.resetCustomAxisMax()
        barChartView.leftAxis.resetCustomAxisMax()
        
    }
    
    public func slider(_ point:CGPoint) {
        self.barChartView.setTouchLocation(point)
    }
    
    public func clearHighlight() {

        self.barChartView.highlightValue(nil)
        
        dataLabel.text = avg.normalTitle
        timeLabel.text = dateString
    
    }
    
    
    public lazy var dataLabel: Label = {
        let label = Label.init()
        label.text = "--"
        label.font = Fonts.systemFont(ofSize: 40, weight: .semibold)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    public lazy var timeLabel: Label = {
        let label = Label.init()
        label.text = "--"
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    public lazy var unitLabel: Label = {
        let label = Label.init()
        label.text = "heart_rate_bpm".language
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()

}


